package binarySearch;

//https://leetcode.cn/problems/binary-search/
public class l_704 {
    public int search(int[] nums, int target) {
        int left = 0,right = nums.length - 1;
        //朴素版本二分
        while(left <= right)
        {
            //防溢出
            //left + (right - left + 1) / 2 等价 (left + right + 1) / 2 朴素版本这里加不加1都一样,
            int mid = left + (right - left) / 2;
            if(nums[mid] < target) left = mid + 1;
            else if(nums[mid] > target) right = mid - 1;
            else return mid;
        }
        return -1;
    }
}

//朴素二分模板
//while(left <= right)
//        {
//            int mid = left + (right - left) / 2;
//            if( ... )
//                  left = mid + 1;
//            else if( ... )
//                  right = mid - 1;
//            else
//                  return ... ;
//        }
